Exercise 2-9.¶

Implement orthogonal vector decomposition. Start with two random-number vectors t and r, and reproduce Figure 2-8 (note that your plot will look somewhat different due to random numbers). Next, confirm that the two components sum to t and that t⊥r and t∥r are orthogonal.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
# Generate random target and reference vectors
target_vec = np.random.randint(1, 7, 2)  # Generates random integers between 1 and 6 for each component
ref_vec = np.random.randint(1, 7, 2)

# Print vectors for verification
print("Target Vector:", target_vec)
print("Reference Vector:", ref_vec)
Target Vector: [6 1]
Reference Vector: [4 3]
In [3]:
# Calculate the parallel component of target_vec to ref_vec
parallel_vec = ref_vec * (np.dot(target_vec, ref_vec) / np.dot(ref_vec, ref_vec))
print("Parallel Component:", parallel_vec)
Parallel Component: [4.32 3.24]
In [4]:
# Calculate the perpendicular component of target_vec to ref_vec
perp_vec = target_vec - parallel_vec
print("Perpendicular Component:", perp_vec)
Perpendicular Component: [ 1.68 -2.24]
In [5]:
# Setup the plot
plt.figure(figsize=(8, 8))
plt.grid()

# Define the origin
origin = np.array([0, 0])
arrow_options = dict(linewidth=0.2, head_width=0.2)

# Draw arrows for target and reference vectors
plt.arrow(*origin, *target_vec, color='r', **arrow_options)
plt.arrow(*origin, *ref_vec, color='g', **arrow_options)

# Add labels to the target and reference vectors
target_label_pos = target_vec + np.array([0.2, 0.2])
ref_label_pos = ref_vec + np.array([0.2, 0.2])
plt.text(*target_label_pos, s=r'$\vec{T}$')
plt.text(*ref_label_pos, s=r'$\vec{R}$')

# Draw arrows for parallel and perpendicular components
plt.arrow(*origin, *parallel_vec, color='b', **arrow_options)
plt.arrow(*origin, *perp_vec, color='y', **arrow_options)

# Label the parallel and perpendicular components
parallel_label_pos = parallel_vec + np.array([0.2, 0.2])
perp_label_pos = perp_vec + np.array([0.2, 0.2])
plt.text(*parallel_label_pos, s=r'$\vec{T}_{\parallel \vec{R}}$')
plt.text(*perp_label_pos, s=r'$\vec{T}_{\perp \vec{R}}$')

# Set axis properties to keep the aspect ratio equal
plt.axis('equal')
plt.show()
In [6]:
# Check orthogonality by calculating dot product (should be close to 0)
dot = np.dot(perp_vec, parallel_vec)
print("Dot Product (should be 0):", dot)
Dot Product (should be 0): -2.0051516003150028e-15
In [7]:
# Verify if the sum of the components equals the original vector
sum_of_comps = parallel_vec + perp_vec
print("Sum of Components:", sum_of_comps)
print("Original Target Vector:", target_vec)
Sum of Components: [6. 1.]
Original Target Vector: [6 1]

Problem Description¶

The exercise involves implementing orthogonal vector decomposition. This method is used to break down a vector r into two orthogonal components relative to another vector t: one component parallel (t∥) and one component perpendicular (t⊥) to t. The goal is to visualize these components and confirm their orthogonality.

What You Did¶

  1. Generate Two Random Vectors: Two vectors t and r in R3 were randomly generated.
  2. Calculate Orthogonal Components:
    • Parallel Component: Computed as the projection of r onto t.
    • Perpendicular Component: Calculated as the difference between r and its projection onto t.
  3. Visualization: Plotted the original vector, basis vector, and both orthogonal components using 3D scatter plots.
    1. Verification: Checked that the sum of the orthogonal components equals r and that these components are indeed orthogonal (dot product close to zero).

Relevant Inputs and Intermediate Values¶

  1. Random Vectors t and r:
    • t: A randomly generated vector serving as the basis.
    • r: Another randomly generated vector to be decomposed.
  2. Calculate the parallel and perpendicular components using their formula.

Exercise 3-3¶

In this exercise, you will draw random points in subspaces. This will help reinforce the idea that subspaces comprise any linear weighted combination of the spanning vectors. Define a vector set containing one vector [1, 3, 3]. Then create 100 numbers drawn randomly from a uniform distribution between -4 and +4. Those are your random scalars. Multiply the random scalars by the basis vector to create 100 random points in the subspace. Plot those points.

Next, repeat the procedure but using two vectors in R3 : [3, 5, 1] and [0, 2, 2]. Note that you need 100 x 2 random scalars for 100 points and two vectors. The resulting random dots will be on a plane. Figure 3-7 shows what the results will look like (it’s not clear from the figure that the points lie on a plane, but you’ll see this when you drag the plot around on your screen).

I recommend using the plotly library to draw the dots, so you can click-drag the 3D axis around. Here’s a hint for getting it to work:

import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter3d(
    x=points[:,0], y=points[:,1], z=points[:,2],
    mode='markers')])
fig.show()

Finally, repeat the R3 case but setting the second vector to be 1/2 times the first.

Outputs¶

  1. 3D Visualization: Displaying r, t, t∥, and t⊥.
  2. Orthogonality Check:
    • A numerical value showing the dot product between t⊥ and t∥, which should be close to zero to confirm orthogonality.
In [8]:
import numpy as np
import plotly.graph_objects as go
In [9]:
# Part 1: Single vector [1, 3, 3]
vector1 = np.array([1, 3, 3])
scalars1 = np.random.uniform(-4, 4, 100)
points1 = scalars1[:, np.newaxis] * vector1
In [10]:
# Create the plot for the single vector
fig1 = go.Figure(data=[go.Scatter3d(
    x=points1[:, 0],
    y=points1[:, 1],
    z=points1[:, 2],
    mode='markers',
    marker=dict(size=4)
)])
fig1.update_layout(title="3D Scatter Plot of Points Along Single Vector [1, 3, 3]")
fig1.show()
In [11]:
# Part 2: Two vectors [3, 5, 1] and [0, 2, 2]
vector2a = np.array([3, 5, 1])
vector2b = np.array([0, 2, 2])
scalars2a = np.random.uniform(-4, 4, 100)
scalars2b = np.random.uniform(-4, 4, 100)
points2 = scalars2a[:, np.newaxis] * vector2a + scalars2b[:, np.newaxis] * vector2b
In [12]:
# Create the plot for two vectors
fig2 = go.Figure(data=[go.Scatter3d(
    x=points2[:, 0],
    y=points2[:, 1],
    z=points2[:, 2],
    mode='markers',
    marker=dict(size=4)
)])
fig2.update_layout(title="3D Scatter Plot of Points on a Plane Formed by Vectors [3, 5, 1] and [0, 2, 2]")
fig2.show()
In [13]:
# Part 3: Two vectors with second vector being 1/2 times the first
vector3a = vector2a
vector3b = 0.5 * vector2a
scalars3a = np.random.uniform(-4, 4, 100)
scalars3b = np.random.uniform(-4, 4, 100)
points3 = scalars3a[:, np.newaxis] * vector3a + scalars3b[:, np.newaxis] * vector3b
In [14]:
# Create the plot for modified two vectors
fig3 = go.Figure(data=[go.Scatter3d(
    x=points3[:, 0],
    y=points3[:, 1],
    z=points3[:, 2],
    mode='markers',
    marker=dict(size=4)
)])
fig3.update_layout(title="3D Scatter Plot of Points on a Line, Second Vector 1/2 the First")
fig3.show()
In [15]:
# Part 1: Single vector [1, 3, 3]
vector1 = np.array([1, 3, 3])
scalars1 = np.random.uniform(-4, 4, 100)
points1 = scalars1[:, np.newaxis] * vector1
In [16]:
# Create the plot for the single vector
fig1 = go.Figure(data=[go.Scatter3d(
    x=points1[:, 0],
    y=points1[:, 1],
    z=points1[:, 2],
    mode='markers',
    marker=dict(size=4)
)])
fig1.update_layout(title="3D Scatter Plot of Points Along Single Vector [1, 3, 3]")

Problem Description¶

In the exercise, we needed to understand the concept of subspaces in vector spaces by visualizing random points that align with given vectors in R3. The goal is to see how linear combinations of vectors form subspaces such as lines and planes.

What I Did¶

1. Single Vector [1, 3, 3]:
    - Defined a vector and generated 100 random scalars.
    - Computed points as linear combinations of the vector scaled by these random values.
    - Plotted these points to visualize a line in three-dimensional space.
2. Two Vectors [3, 5, 1] and [0, 2, 2]:
    - Used two vectors and generated 200 random scalars (100 for each vector).
    - Computed points as linear combinations of these vectors using the random scalars.
    - Plotted these points to observe the formation of a plane in three-dimensional space.
3. Modified Two Vectors Case:
    - Repeated the two-vector scenario but with the second vector being half the first vector.
    - This was meant to show that the points still lie on a line despite using two vectors, due to the linear dependency between the vectors.

Relevant Inputs and Intermediate Values¶

Random Scalars: Generated using np.random.uniform(-4, 4, 100), which produces numbers between -4 and 4.
Vectors:
    - For the line: [1,3,3]
    - For the plane: [3,5,1] and [0,2,2] 
    - For the line with dependent vectors: [3,5,1] and [1.5,2.5,0.5] (which is half of the first vector).

Outputs¶

3D Scatter Plots: These plots visually represent the points in the subspace formed by the given vectors. The plots allow interaction such as rotation to view the alignment of points in 3D space.

Below You will be able to find Github Link to my week 2 lab assignment

https://github.com/kirtishrestha/MTH225

To see the output figures please run the cell block.